

package com.googlecode.flac4j.format.metadata;


import java.util.SortedMap;
import java.util.TreeMap;

import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.adapters.XmlAdapter;


/**
 * XmlAdapter for a map of numbers and indices.
 *
 * @author Jin Kwon <jinahya at gmail.com>
 */
@XmlTransient
public class IndicesAdapter
    extends XmlAdapter<Indices, SortedMap<Integer, Index>> {


    @Override
    public Indices marshal(final SortedMap<Integer, Index> bound)
        throws Exception {

        final Indices value = new Indices();
        value.getIndices().addAll(bound.values());

        return value;
    }


    @Override
    public SortedMap<Integer, Index> unmarshal(final Indices value)
        throws Exception {

        final SortedMap<Integer, Index> bound = new TreeMap<Integer, Index>();
        for (Index Index : value.getIndices()) {
            bound.put(Index.getNumber(), Index);
        }

        return bound;
    }


}

